package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.consume.domain.dao.ConsumeAuditConfig;
import com.eclink.asset.consume.domain.dao.ConsumeAuditConfigNode;
import com.eclink.asset.consume.mapper.ConsumeAuditConfigMapper;
import com.eclink.asset.consume.mapper.ConsumeAuditConfigNodeMapper;
import com.eclink.asset.pojo.query.AssetAuditConfigQuery;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 耗材审批流程Service业务层处理
 *
 *
 * @author chenshixian
 */
@Service
public class ConsumeAuditConfigService {

    @Resource
    private ConsumeAuditConfigMapper consumeAuditConfigMapper;

    @Resource
    private ConsumeAuditConfigNodeMapper consumeAuditConfigNodeMapper;

    /**
     * 查询
     *
     * @param id ID
     * @return 结果
     */
    public ConsumeAuditConfig selectById(Integer id) {
        return consumeAuditConfigMapper.selectById(id);
    }

    public ConsumeAuditConfig selectByOrderType(String orderType) {
        QueryWrapper<ConsumeAuditConfig> qw = new QueryWrapper<>();
        qw.eq("order_type", orderType);
        return consumeAuditConfigMapper.selectOne(qw);
    }

    public List<ConsumeAuditConfigNode> getNodes(Long configId) {
        QueryWrapper<ConsumeAuditConfigNode> qw = new QueryWrapper<>();
        qw.eq("config_id", configId);
        return consumeAuditConfigNodeMapper.selectList(qw);
    }


    /**
     * 查询【请填写功能名称】列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 【请填写功能名称】
     */
    public List<ConsumeAuditConfig> selectList(AssetAuditConfigQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ConsumeAuditConfig> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String configNameLike = query.getConfigNameLike();
        if (!StringUtils.isEmpty(configNameLike)) {
            qw.like("config_name", configNameLike);
        }
        String configDesc = query.getConfigDesc();
        if (!StringUtils.isEmpty(configDesc)) {
            qw.eq("config_desc", configDesc);
        }
        String orderType = query.getOrderType();
        if (!StringUtils.isEmpty(orderType)) {
            qw.eq("order_type", orderType);
        }
        Integer status = query.getStatus();
        if (status != null) {
            qw.eq("status", status);
        }
        Integer createUserId = query.getCreateUserId();
        if (createUserId != null) {
            qw.eq("create_user_id", createUserId);
        }
        Integer updateUserId = query.getUpdateUserId();
        if (updateUserId != null) {
            qw.eq("update_user_id", updateUserId);
        }
        return consumeAuditConfigMapper.selectList(qw);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param assetAuditConfig 【请填写功能名称】
     * @return 结果
     */
    public int insert(ConsumeAuditConfig assetAuditConfig) {
        assetAuditConfig.setDelFlag(0);
        assetAuditConfig.setCreateTime(LocalDateTime.now());
        return consumeAuditConfigMapper.insert(assetAuditConfig);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param assetAuditConfig 【请填写功能名称】
     * @return 结果
     */
    public int update(ConsumeAuditConfig assetAuditConfig) {
        return consumeAuditConfigMapper.updateById(assetAuditConfig);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        for(Long id : ids) {
            consumeAuditConfigMapper.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return consumeAuditConfigMapper.deleteById(id);
    }
}
